/*
 * AddTagAction.java
 *
 * Created on 5 de Dezembro de 2007, 19:48
 */

package madsgtdinterface;

import com.sun.jmx.snmp.tasks.Task;
import java.util.LinkedList;
import thunderbirdgtd.entities.Action;
import thunderbirdgtd.entities.Tag;
import thunderbirdgtd.GTDParent;
import java.awt.Color;
import java.awt.Frame;
import java.util.ListIterator;
import javax.swing.DefaultListModel;
import javax.swing.JDialog;
import javax.swing.JList;
import thunderbirdgtd.entities.BaseActionOrProject;
import thunderbirdgtd.entities.Project;

/**
 *
 * @author  Ricardo Cruz
 */
public class AddTagAction extends javax.swing.JDialog {
    
    private BaseActionOrProject actiona;
    private LinkedList<Tag> tagsTotal = new  LinkedList<Tag>();
    private LinkedList<Tag> tagsAction = new  LinkedList<Tag>();
    private GTDParent gtd;
    /** Creates new form AddTagAction */
    public AddTagAction(java.awt.Frame parent, boolean modal, BaseActionOrProject actiona, GTDParent gt) {
        
        super(parent, modal);
        this.actiona = actiona;
        initComponents();
        gtd=gt;

        tagsTotal = gtd.tags;
       
        jLabel1.setText(actiona.getName());
          
        tagsAction = actiona.getTags();
        
        ListIterator<Tag> tagsIteratorAction = tagsAction.listIterator();
        DefaultListModel lItemsAction = new DefaultListModel();
        
         DefaultListModel lItemsTotal = new DefaultListModel();
        ListIterator<Tag> tagsIteratorTotal = gtd.tags.listIterator();
        
        while(tagsIteratorAction.hasNext()){
              lItemsAction.addElement(tagsIteratorAction.next().getTagName());
        }
        
        jList1.setModel(lItemsAction);
        

        
        while(tagsIteratorTotal.hasNext()){
              //for(int i=0; i < lItemsAction.getSize(); i++){
              //    String tagName = tagsIteratorTotal.next().getTagName();
              //    if(!lItemsAction.get(i).equals(tagName))
                        lItemsTotal.addElement(tagsIteratorTotal.next().getTagName());
              //}
        }

        jList2.setModel(lItemsTotal);
        
    }
    
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        jList1 = new javax.swing.JList();
        jScrollPane2 = new javax.swing.JScrollPane();
        jList2 = new javax.swing.JList();
        jButton1 = new javax.swing.JButton();
        jButton2 = new javax.swing.JButton();
        jButton3 = new javax.swing.JButton();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

        jLabel1.setText("Nome da Tarefa");

        jScrollPane1.setViewportView(jList1);

        jList2.setModel(new javax.swing.AbstractListModel() {
            String[] strings = { "Item 1", "Item 2", "Item 3", "Item 4", "Item 5" };
            public int getSize() { return strings.length; }
            public Object getElementAt(int i) { return strings[i]; }
        });
        jScrollPane2.setViewportView(jList2);

        jButton1.setText("<--");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        jButton2.setText("-->");
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });

        jButton3.setText("Add Alarm");
        jButton3.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton3ActionPerformed(evt);
            }
        });

        jLabel2.setText("Action Tag");

        jLabel3.setText("Tags");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(36, 36, 36)
                        .addComponent(jLabel1))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addGroup(layout.createSequentialGroup()
                                .addContainerGap()
                                .addComponent(jButton3))
                            .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                                .addGap(39, 39, 39)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addGroup(layout.createSequentialGroup()
                                        .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 111, javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addGap(18, 18, 18)
                                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                            .addComponent(jButton1)
                                            .addComponent(jButton2)))
                                    .addComponent(jLabel2))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 18, Short.MAX_VALUE)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(jLabel3)
                                    .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 123, javax.swing.GroupLayout.PREFERRED_SIZE))))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)))
                .addContainerGap(42, javax.swing.GroupLayout.PREFERRED_SIZE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(25, 25, 25)
                .addComponent(jLabel1)
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(jLabel3))
                .addGap(3, 3, 3)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jScrollPane2, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 180, Short.MAX_VALUE)
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                        .addGroup(layout.createSequentialGroup()
                            .addGap(18, 18, 18)
                            .addComponent(jButton1)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                            .addComponent(jButton2))
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 180, Short.MAX_VALUE)))
                .addGap(12, 12, 12)
                .addComponent(jButton3)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
        // TODO add your handling code here:
 
       
        try{
           // Tag tagAdd = Tag.searchTagInLinkedList(jList2.getSelectedValue().toString(), tagsTotal);
            System.out.println("rolhas");
            System.out.println(jList1.getSelectedValue());
           actiona.removeTag(jList1.getSelectedValue().toString());
            
        }
        catch(Exception e){
            e.printStackTrace();
        }
       
        ListIterator<Tag> tagsIteratorAction = tagsAction.listIterator();
        
        DefaultListModel lItemsAction = new DefaultListModel();
        
        while(tagsIteratorAction.hasNext()){
              lItemsAction.addElement(tagsIteratorAction.next().getTagName());
        }
        
        jList1.setModel(lItemsAction);
        
         DefaultListModel lItemsTotal = new DefaultListModel();
        ListIterator<Tag> tagsIteratorTotal = gtd.tags.listIterator();
        
        while(tagsIteratorTotal.hasNext()){
             // for(int i=0; i < lItemsAction.getSize(); i++){
             //     String tagName = tagsIteratorTotal.next().getTagName();
             //     if(!lItemsAction.get(i).equals(tagName))
                        lItemsTotal.addElement(tagsIteratorTotal.next().getTagName());
                //   else
                 //     tagsTotal.remove(i);
                      
              }

        jList2.setModel(lItemsTotal);
    }//GEN-LAST:event_jButton2ActionPerformed

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        // TODO add your handling code here:
  
        try{
            System.out.println(jList2.getSelectedValue());
           Tag tagAdd = Tag.searchTagInLinkedList(jList2.getSelectedValue().toString(), tagsTotal);
           System.out.println(tagAdd.getTagName());
           actiona.addTag(tagAdd);
           System.out.println(jList2.getSelectedValue());
        }
        catch (Exception e){
            e.printStackTrace();
        }

       // tagsAction = actiona.getTags();
        ListIterator<Tag> tagsIteratorAction = tagsAction.listIterator();
        
        DefaultListModel lItemsAction = new DefaultListModel();
        
        while(tagsIteratorAction.hasNext()){
              lItemsAction.addElement(tagsIteratorAction.next().getTagName());
        }
        
        
        DefaultListModel lItemsTotal = new DefaultListModel();
        ListIterator<Tag> tagsIteratorTotal = gtd.tags.listIterator();
        
        while(tagsIteratorTotal.hasNext()){
               lItemsTotal.addElement(tagsIteratorTotal.next().getTagName());

        }
        
      //  for(int i=0; i < lItemsTotal.size(); i++){
            
          //  if(lItemsAction.get(i)
           // lItemsTotal.removeElementAt(i);
       // }
        
         jList1.setModel(lItemsAction);
        jList2.setModel(lItemsTotal);
        
        //actiona.addTag(arg0);
    }//GEN-LAST:event_jButton1ActionPerformed

    private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton3ActionPerformed
        // TODO add your handling code here:
        AddTag tW = new AddTag((JDialog)this, true, gtd.tags);
        tW.setVisible(true);
    }//GEN-LAST:event_jButton3ActionPerformed
    
    /**
     * @param args the command line arguments
     */
  
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JButton jButton3;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JList jList1;
    private javax.swing.JList jList2;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    // End of variables declaration//GEN-END:variables
    
}
